package com.googlecode.lookie.platform;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.richclient.application.config.ApplicationWindowAware;

/*
 * A bean post processor that will set the {@link ApplicationWindow} of any beans implementing the
 * {@link ApplicationWindowAware} interface.
 * 
 * @author Keith Donald
 * 
 * @see BeanPostProcessor
 */
class PlatformWindowSetter
    implements BeanPostProcessor
{

    private PlatformLifecycleAdvisor lifecycleAdvisor;

    public PlatformLifecycleAdvisor getLifecycleAdvisor()
    {
        return lifecycleAdvisor;
    }

    public void setLifecycleAdvisor( PlatformLifecycleAdvisor lifecycleAdvisor )
    {
        this.lifecycleAdvisor = lifecycleAdvisor;
    }

    /**
     * Default implementation, performs no action on the given bean.
     */
    public Object postProcessAfterInitialization( Object bean, String beanName )
        throws BeansException
    {
        return bean;
    }

    /**
     * If the given bean is an implementation of {@link ApplicationWindowAware}, it will have its
     * application window set to the window provided to this instance at construction time.
     *
     */
    public Object postProcessBeforeInitialization( Object bean, String beanName )
        throws BeansException
    {
        if ( bean instanceof ApplicationWindowAware )
        {
            ( (ApplicationWindowAware) bean ).setApplicationWindow( getLifecycleAdvisor().getWindow() );
        }
        return bean;
    }

}
